/* 
Copyright 2005-2009, Foundations of Success, Bethesda, Maryland 
(on behalf of the Conservation Measures Partnership, "CMP") and 
Beneficent Technology, Inc. ("Benetech"), Palo Alto, California. 

This file is part of Miradi

Miradi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, 
as published by the Free Software Foundation.

Miradi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Miradi.  If not, see <http://www.gnu.org/licenses/>. 
*/ 

package org.miradi.diagram.coalescer;

import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import org.miradi.commands.CommandSetObjectData;
import org.miradi.exceptions.CommandFailedException;
import org.miradi.exceptions.UnexpectedNonSideEffectException;
import org.miradi.main.TestCaseWithProject;
import org.miradi.objectdata.BooleanData;
import org.miradi.objecthelpers.ORef;
import org.miradi.objecthelpers.ORefList;
import org.miradi.objecthelpers.ObjectType;
import org.miradi.objects.Cause;
import org.miradi.objects.DiagramFactor;
import org.miradi.objects.DiagramLink;
import org.miradi.objects.GroupBox;
import org.miradi.objects.Strategy;
import org.miradi.objects.Target;

public class TestCoalescer extends TestCaseWithProject
{
	public TestCoalescer(String name)
	{
		super(name);
	}

	

	public void testSkipAlreadyGroupedTargets() throws Exception
	{
		DiagramFactor threatDiagramFactor1 = createThreat();
		DiagramFactor threatDiagramFactor2 = createThreat();
		DiagramFactor targetDiagramFactor1 = createTarget();
		DiagramFactor targetDiagramFactor2 = createTarget();
		
		getProject().createDiagramFactorLinkAndAddToDiagram(threatDiagramFactor1, targetDiagramFactor1);
		getProject().createDiagramFactorLinkAndAddToDiagram(threatDiagramFactor2, targetDiagramFactor1);
		
		ORefList threats1And2 = new ORefList(new DiagramFactor[] {threatDiagramFactor1, threatDiagramFactor2});

		DiagramFactor groupDiagramFactor = getProject().createDiagramFactorAndAddToDiagram(GroupBox.getObjectType());
		CommandSetObjectData addChildren = new CommandSetObjectData(groupDiagramFactor.getRef(), DiagramFactor.TAG_GROUP_BOX_CHILDREN_REFS, threats1And2.toString());
		getProject().executeCommand(addChildren);
		
		getProject().createDiagramFactorLinkAndAddToDiagram(groupDiagramFactor, targetDiagramFactor2);
		getProject().createDiagramFactorLinkAndAddToDiagram(threatDiagramFactor2, targetDiagramFactor2);

		final Vector<DiagramLink> links = getDiagramModel().getAllDiagramFactorLinks();
		try{
			for (DiagramLink link : links)
			{
				System.err.println("From: " + link.getFromDiagramFactor().getTypeName() + " To: " + link.getToDiagramFactor().getTypeName());
				System.err.println("From: " + ObjectType.getUserFriendlyObjectTypeName(getProject(), link.getFromDiagramFactor().getWrappedType()) + " To: " + ObjectType.getUserFriendlyObjectTypeName(getProject(), link.getToDiagramFactor().getWrappedType()));
				System.err.println();
			} 			
		} catch (Exception e) {
			//...
		}
				
	}
	
	private DiagramFactor createStrategy() throws Exception
	{
		return getProject().createDiagramFactorAndAddToDiagram(Strategy.getObjectType());
	}

	private DiagramFactor createThreat() throws Exception, UnexpectedNonSideEffectException, CommandFailedException
	{
		DiagramFactor threatDiagramFactor = getProject().createDiagramFactorAndAddToDiagram(Cause.getObjectType());
		ORef threatRef = threatDiagramFactor.getWrappedORef();
		CommandSetObjectData toThreat = new CommandSetObjectData(threatRef, Cause.TAG_IS_DIRECT_THREAT, BooleanData.BOOLEAN_TRUE);
		getProject().executeCommand(toThreat);
		return threatDiagramFactor;
	}
	
	private DiagramFactor createTarget() throws Exception
	{
		return getProject().createDiagramFactorAndAddToDiagram(Target.getObjectType());
	}

	private DiagramFactor createEmptyGroupBox() throws Exception
	{
		return getProject().createDiagramFactorAndAddToDiagram(GroupBox.getObjectType());
	}

	private DiagramFactor getGroup(DiagramFactor diagramfactor)
	{
		ORef groupRef = diagramfactor.findObjectsThatReferToUs(DiagramFactor.getObjectType()).get(0);
		DiagramFactor group = DiagramFactor.find(getProject(), groupRef);
		return group;
	}
	
}
